#!/usr/bin/env python3

import os

# Path to ffconf.h file
ffconf_path = 'G:\\Espressif\\frameworks\\esp-idf-v5.5.1\\components\\fatfs\\src\\ffconf.h'

def fix_lfn():
    print(f"Fixing LFN support in {ffconf_path}...")
    
    # Check if file exists
    if not os.path.exists(ffconf_path):
        print(f"File not found: {ffconf_path}")
        return False
    
    # Read file content
    with open(ffconf_path, 'r') as f:
        content = f.read()
    
    # Fix FF_USE_LFN - ensure it's set to 3 (use heap for LFN)
    # First, remove all existing FF_USE_LFN definitions
    lines = content.split('\n')
    filtered_lines = []
    for line in lines:
        if 'FF_USE_LFN' not in line or not line.strip().startswith('#define'):
            filtered_lines.append(line)
    
    # Now add the correct FF_USE_LFN definition after FF_USE_STRFUNC
    fixed_lines = []
    for line in filtered_lines:
        fixed_lines.append(line)
        if 'FF_USE_STRFUNC' in line and line.strip().startswith('#define'):
            # Add FF_USE_LFN after FF_USE_STRFUNC
            fixed_lines.append('#define FF_USE_LFN    3')
    
    # Join lines back into content
    fixed_content = '\n'.join(fixed_lines)
    
    # Write fixed content back to file
    with open(ffconf_path, 'w') as f:
        f.write(fixed_content)
    
    print("Fixed FF_USE_LFN to 3")
    return True

if __name__ == "__main__":
    fix_lfn()
